package coc.example.utils;

import java.util.Map;

import coc.core.CrawlerConfig;
import coc.core.collector.CollectorType;
import coc.core.util.BrowserVersionUtils;
import coo.base.model.Params;

/**
 * 固定参数工具类。
 */
public class FixedParamsUtils {
	private static final String THREAD_COUNT = "threadCount";
	private static final String JOB_TIMEOUT = "jobTimeout";
	private static final String TIMEOUT = "timeout";
	private static final String MAX_DEPTH = "maxDepth";
	private static final String INTERVAL = "interval";
	private static final String WAIT_FOR_GET_PAGE = "waitForGetPage";
	private static final String COLLECTOR_TYPE = "collectorType";
	private static final String BROWSER_VERSION = "browserVersion";
	private static final String COLLECTOR_TERMS = "collectorTerms";

	/**
	 * 新建默认固定参数。
	 * 
	 * @return 返回默认固定参数。
	 */
	public static Params newFixedParams() {
		return toFixedParams(new CrawlerConfig());
	}

	/**
	 * 将爬虫配置转换为固定参数。
	 * 
	 * @param config
	 *            爬虫配置
	 * @return 返回转换的固定参数。
	 */
	public static Params toFixedParams(CrawlerConfig config) {
		Params params = new Params();
		params.put(THREAD_COUNT, config.getThreadCount().toString());
		params.put(JOB_TIMEOUT, config.getJobTimeout().toString());
		params.put(TIMEOUT, config.getTimeout().toString());
		params.put(MAX_DEPTH, config.getMaxDepth().toString());
		params.put(INTERVAL, config.getInterval().toString());
		params.put(WAIT_FOR_GET_PAGE, config.getWaitForGetPage().toString());
		params.put(COLLECTOR_TYPE, config.getCollectorType().name());
		params.put(BROWSER_VERSION, config.getBrowserVersion().getNickname());
		params.putMap(COLLECTOR_TERMS, config.getCollectorTerms());
		return params;
	}

	/**
	 * 将固定参数转换成爬虫配置。
	 * 
	 * @param params
	 *            固定参数
	 * @return 返回转换的爬虫配置。
	 */
	public static CrawlerConfig toCrawlerConfig(Params params) {
		CrawlerConfig config = new CrawlerConfig();
		Integer threadCount = params.getInteger(THREAD_COUNT);
		if (threadCount != null) {
			config.setThreadCount(threadCount);
		}
		Integer jobTimeout = params.getInteger(JOB_TIMEOUT);
		if (jobTimeout != null) {
			config.setJobTimeout(jobTimeout);
		}
		Integer timeout = params.getInteger(TIMEOUT);
		if (timeout != null) {
			config.setTimeout(timeout);
		}
		Integer maxDepth = params.getInteger(MAX_DEPTH);
		if (maxDepth != null) {
			config.setMaxDepth(maxDepth);
		}
		Integer interval = params.getInteger(INTERVAL);
		if (interval != null) {
			config.setInterval(interval);
		}
		Integer waitForGetPage = params.getInteger(WAIT_FOR_GET_PAGE);
		if (waitForGetPage != null) {
			config.setWaitForGetPage(waitForGetPage);
		}
		String collectorType = params.get(COLLECTOR_TYPE);
		if (collectorType != null) {
			config.setCollectorType(Enum.valueOf(CollectorType.class,
					collectorType));
		}
		String browserVersion = params.get(BROWSER_VERSION);
		if (browserVersion != null) {
			config.setBrowserVersion(BrowserVersionUtils
					.getBrowserVersion(browserVersion));
		}
		Map<String, String> collectorTerms = params.getMap(COLLECTOR_TERMS);
		if (!collectorTerms.isEmpty()) {
			config.setCollectorTerms(collectorTerms);
		}
		return config;
	}
}
